package cn.edu.bupt.thymeleaf.examples.controller;

import cn.edu.bupt.thymeleaf.examples.entities.User;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class UserController {
    @GetMapping("/login")
    public String loginPage() {
        return "login";
    }

    @PostMapping("/login")
    public String login(@RequestParam("email") String email, @RequestParam("password") String password, HttpServletRequest request) {
        // 这里应该有验证逻辑
        if ("admin@123.com".equals(email) && "123456".equals(password)) {
            // 登录成功，将用户信息存入session
            request.getSession(true).setAttribute("user", new User("admin", "Administrator", "China", 25));
            return "redirect:/";
        } else {
            // 登录失败，返回登录页面
            return "login";
        }
    }

    @GetMapping("/register")
    public String registerPage() {
        return "register";
    }

    @PostMapping("/register")
    public String register(@RequestParam("username") String username, @RequestParam("email") String email, @RequestParam("password") String password) {
        System.out.println("Registered user: " + username + ", " + email + ", " + password);
        // 注册逻辑，暂时略
        return "redirect:/login";
    }

    @GetMapping("/signOut")
    public String signOut(HttpServletRequest request) {
        request.getSession(true).removeAttribute("user");
        return "redirect:/login";
    }
    
    @GetMapping("/userprofile")
    public String getUserProfile() {
        return "userprofile";
    }
}